package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.FormDetail;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.List;

public class FormChildAdapter extends ArrayAdapter<FormDetail> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-06-03 16:03:34 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView mTvNameFormDetail;
    public final TextView mTvValueFormDetail;

    private ViewHolder(LinearLayout rootView, TextView mTvNameFormDetail,
        TextView mTvValueFormDetail) {
      this.rootView = rootView;
      this.mTvNameFormDetail = mTvNameFormDetail;
      this.mTvValueFormDetail = mTvValueFormDetail;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView mTvNameFormDetail = (TextView) rootView.findViewById(R.id.tv_name_formDetail);
      TextView mTvValueFormDetail = (TextView) rootView.findViewById(R.id.tv_value_formDetail);
      return new ViewHolder(rootView, mTvNameFormDetail, mTvValueFormDetail);
    }
  }

  private static final String TAG = "TestAdapter";

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_form_detail, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    FormDetail item = getItem(position);

    if (mTitlesList.contains(item.getName())) {
      vh.mTvNameFormDetail.setTextColor(parent.getResources().getColor(R.color.blue_click));
    }
    vh.mTvNameFormDetail.setText(item.getName());
    if (item == null || item.getValue() == null || item.getValue().equalsIgnoreCase("null")) {
      vh.mTvValueFormDetail.setText("");
    } else {
      vh.mTvValueFormDetail.setText(item.getValue());
    }
    getItem(position);
    return vh.rootView;
  }

  private LayoutInflater mInflater;
  private ArrayList<FormDetail> mTitlesList;

  // Constructors
  public FormChildAdapter(Context context, List<FormDetail> objects, ArrayList<FormDetail> titles) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mTitlesList = titles;
  }

  public FormChildAdapter(Context context, FormDetail[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  @Override public int getViewTypeCount() {
    return 2;
  }

  @Override public int getItemViewType(int position) {
    int type;
    if (getItem(position).getName() != null && mTitlesList.contains(getItem(position).getName())) {
      type = 1;
    } else {
      type = 0;
    }
    LogUtil.i(TAG, "type=" + type);
    return type;
  }
}
